import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
    count: 0 
}
const getters = {
    evenOrOdd: state => state.count%2 == 0?'even':'odd'
}
const mutations = {
    increment_(state){
        state.count++
    }
}
const actions = {
    // increment (context) {
    //     context.commit('increment_')
    //   }
       increment: ({commit}) =>{
     
        commit('increment_')
        console.log(state.count) 
       },
       incrementIfOdd ({ commit, state }) {
           console.log(state.count) 
        if ((state.count) % 2!=0) {
          commit('increment_')
        }
        },
        incrementAsync ({ commit }) {
            return new Promise((resolve, reject) => {
              setTimeout(() => {
                commit('increment_')
                resolve()
              }, 1000)
            })
          }

}

// A Vuex instance is created by combining the state, mutations, actions,
// and getters.
export default new Vuex.Store({
    state,
    getters,
    actions,
    mutations
  })
  